package cn.itsource.util;

import cn.hutool.json.JSONUtil;
import cn.itsource.bean.LoginInfo;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

import static cn.itsource.util.RedisContants.LOGIN_USER_KEY;

/**
 * @author : 张金铭
 * @description : 自定义工具类
 * @create :2022-11-09 09:05:00
 */
public class MyUtils {


    @Resource
    private StringRedisTemplate stringRedisTemplate;


    //将集合转化为/12/32/xxx  的形式str
    public static String getDir(List<Long>parentInts ){

        String dirPath = "/";
        //有上级部门情况
        if (parentInts!=null&&parentInts.size()>0){
            for (int i = 0; i < parentInts.size(); i++) {
                if (i!=parentInts.size()-1) {
                    dirPath = dirPath + parentInts.get(i) + "/";
                }else {
                    dirPath = dirPath + parentInts.get(i);
                }
            }
        }
        return dirPath;
    }

    /**
     * 将str转化为list集合
     * @param dirPath
     * @return
     */
    public static List<Long> getList(String dirPath){
        ArrayList<Long> list = new ArrayList<>();
        //从前往后截取   /10/15/8/6
        if (dirPath!=null){
            //已知 当不为空时 第一位永远都是 /
            // 所以从1开始往后查 /  当存在/时，会返回/的索引
            //不存在/时会返回-1
            int index = 1;
            //从1开始，包括1往后查指定索引 indexOf为/所在的索引
            while (index != -1) {
                int indexOf = dirPath.indexOf("/", index);
                if (indexOf != -1) {
                    //截取1 到/前的字符串，不包含/
                    String substring = dirPath.substring(index, indexOf);
                    Long valueOf = Long.valueOf(substring);
                    list.add(valueOf);
                    //下次index 就是/的位置+1
                    index = indexOf+1;
                }
                //当indexOf为-1说明已经到最后一个数了，直接截取index到lenght的内容即可
                else {
                    //最后一个是自己不用加入数组
//                    String substring = dirPath.substring(index, dirPath.length());
//                    Long valueOf = Long.valueOf(substring);
//                    list.add(valueOf);
                    //下一次循环会自动停止
                    index = indexOf;
                }
            }
        }
        return list;
    }


    /**
     * 验证所选择上级部门是否包含自己
     * @param id
     * @param parents
     * @return
     */
    public static boolean isRepeat(Long id,List<Long> parents) {
        for (Long parentId:parents
        ) {
            if (parentId.equals(id)){
                return true;
            }
        }
        return false;
    }


    /**
     * 手机号验证
     * @param phoneNumber
     * @return
     */
    public static boolean isValidPhoneNumber(String phoneNumber) {
        if ((phoneNumber != null) && (!phoneNumber.isEmpty())) {
            return Pattern.matches("^1[3-9]\\d{9}$", phoneNumber);
        }
        return false;
    }


    public static String getSalt(){
       return UUID.randomUUID().toString().toUpperCase();
    }


    public static Long getUid(StringRedisTemplate stringRedisTemplate){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String key =LOGIN_USER_KEY + request.getHeader("token");
        String user = stringRedisTemplate.opsForValue().get(key);
        LoginInfo loginInfo = JSONUtil.toBean(user, LoginInfo.class);
        return loginInfo.getId();
    }

}
